package com.gg.web;


import com.gg.pojo.Book;
import com.gg.pojo.Page;
import com.gg.service.BookService;
import com.gg.service.impl.BookServiceImpl;
import com.gg.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();

    /**
     * 处理分页功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数pageNO 和 pageSize 转为int型
        int pageNo = WebUtils.parserInt(req.getParameter("pageNo"),1);
        int pageSize = WebUtils.parserInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用BookService.page(pageNo,pageSize):Page对象
        Page<Book> page = bookService.page(pageNo, pageSize);
            //设置后台分页方法的地址
            page.setUrl("manager/bookServlet?action=page");
        //3.保存Page对象到Request域中,也就是Page对象的五个属性
        req.setAttribute("page",page);
        //4.请求转发到pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }

    //添加书
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       int pageNo = WebUtils.parserInt(req.getParameter("pageNo"),1);
       pageNo += 1; //确保添加图书超了总页数的话就会跳到最后一页
        //1.获取请求参数 ，封装称为Book对象
        Book book = WebUtils.copyParamToBean(req.getParameterMap(),new Book());
        //2.调用BookService.addBook()保存图书到数据库
        bookService.addBook(book);
        //3.跳回图书列表，并调用list()刷新数据  /manager/bookServlet?action=list
        //req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req,resp);
        //请求重定向会由按F5刷新页面再次请求add的Bug，用请求转发就不会
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo=" + pageNo);
    }

    //删除书
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数id
        int id = WebUtils.parserInt(req.getParameter("id"),0);
        //2.调用deleteBookById()删除图书
        bookService.deleteBookById(id);
        //3.重定向回图书列表管理页面,并途径BookServlet /manager/bookServlet?action=list
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    //更改书信息
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数，封装为Book对象
        Book book = WebUtils.copyParamToBean(req.getParameterMap(), new Book());
        //2.调用updateBook();操作数据库修改图书
        bookService.updateBook(book);
        //3.重定向回图书列表页面，并途径BookServlet调用list()刷新
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    //查询全部图书
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.通过BookService查询全部图书
        List<Book> books = bookService.queryBooks();
        //2.把全部图书存储到Request域中
        req.setAttribute("books",books);
        //3.请求转发到/pages/manager/book_manager.jsp页面,同时把数据带过去
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }

    //获取一本书
    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取图书编号
        int id = WebUtils.parserInt(req.getParameter("id"),0);
        //2.调用queryBookById(id)得到修改的图书信息
        Book book = bookService.queryBookById(id);
        //3.把图书信息保存到Request域中
        req.setAttribute("book",book);
        //4.请求转发到/pages/manager/book_edit.jsp页面
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }
}
